<?php
/*  框架视图处理类
 *  ['name'=>'引擎名称','option'=>'引擎初始化参数']
 *  或者 回调函数自定义
 */
namespace hyang;
class View{
    public $v;// 视图对象
    private $_dir;// 目录
    private $_ViewEngine;// 视图引擎
    private $_option;
    private $EngineType = array(
        'smarty','twig','mustache','plates'
    );
    public function __construct($opt=null)
    {
        $name = '';
        if(defined('View_Engine')) $name = View_Engine;
        if(isset($opt['name'])){
            $name = $opt['name'];
            if(isset($opt['option'])) $this->_option = $opt['option'];
        }
        $dir = APP_ROOT.'/ext/'.$name.'/';
        $this->_ViewEngine = '';
        if(is_callable($opt)){// 回调函数用于测试
            $obj = $opt();
            if(is_object($obj)) $this->v = $obj;
            else{
                if(isDPattern()) debug('回调行数“'.$opt.'”无效！来源消息：'.__CLASS__);
            }
        }
        elseif(is_dir($dir) && $name){
            $this->_dir = $dir;
            $method = $name;
            // 预处理
            $this->_methodFind($method);
            if(method_exists($this,$method)){
                $this->_ViewEngine = $method;
                $this->v = $this->$method();                
            }else{
                if(isDPattern()) debug('无法加载模板引擎为“'.$method.'”的视图库！来源消息：'.__CLASS__);
            }
        }
    }
    // 获取视图对象
    public function getObject()
    {
        if(is_object($this->v)) return $this->v;
        return '';
    }
    // 获取引擎名称
    public function getVEName(){return $this->_ViewEngine;}
    // 视图引擎模板处理
    public function _methodFind(&$mthd)
    {
        if(method_exists($this,$mthd)) return true;// 完全匹配
        if(method_exists($this,ucfirst(strtolower($mthd)))){// 不分大小写
            $mthd = ucfirst(strtolower($mthd));
            return true;
        }
        foreach($this->EngineType as $v){// 模糊匹配
            if(substr_count(strtolower($mthd),$v)>0){
                $mthd = ucfirst($v);
                return true;
            }
        }
        return false;
    }
    /* 
        引擎官网 http://mustache.github.io/
        参考： https://github.com/bobthecow/mustache.php/wiki
        多语言模板引擎 - Mustache
    */
    public function Mustache()
    {
        require $this->_dir.'Autoloader.php';
        \Mustache_Autoloader::register();
        if(is_array($this->_option)) $opt = $this->_option;
        elseif(is_string($this->_option)) $opt = config('Mustache_opt',null,'config_mustache');
        else $opt = config('Mustache_opt');
        if(is_array($opt)) $m = new \Mustache_Engine($opt);
        else $m = new \Mustache_Engine;
        return $m;
    }
    /*
        引擎官网：http://www.smarty.net/
        参考： http://www.cnblogs.com/gbyukg/archive/2012/06/12/2542530.html
        Smarty
    */
    public function Smarty()
    {
        require $this->_dir.'Smarty.class.php';
        if(is_array($this->_option)) $opt = $this->_option;
        else $opt = config('Smarty_opt');
        $m = new \Smarty();
        if(isset($opt['cache_dir'])) $m->cache_dir = $opt['cache_dir'];
        if(isset($opt['compile_dir'])) $m->compile_dir = $opt['compile_dir'];
        if(isset($opt['debugging'])) $m->debugging = $opt['debugging'];
        if(isset($opt['caching'])) $m->caching = $opt['caching'];
        if(isset($opt['cache_lifetime'])) $m->cache_lifetime = $opt['cache_lifetime'];
        return $m;
    }
    /*
        引擎官网: http://twig.sensiolabs.org/
        Twig
    */
    public function Twig()
    {
        require $this->_dir.'Autoloader.php';
        \Twig_Autoloader::register();
        // 生成option
        if(is_array($this->_option)) $opt = $this->_option;
        else $opt = config('Twig_opt');
        // 视图目录
        if(isset($opt['view_dir'])) $loader = new \Twig_Loader_Filesystem($opt['view_dir']);
        else $loader = new \Twig_Loader_String();
        // 缓存目录设置
        if(isset($opt['cache_dir'])) $twig = new \Twig_Environment($loader,['cache' => $opt['cache_dir']]);
        else $twig = new \Twig_Environment($loader);
        // 其他开启项
        if(isset($opt['debugging'])) $twig->addExtension(new \Twig_Extension_Debug());
        return $twig;
    }
    /*
        引擎官网: http://platesphp.com
        Plates  原生PHP引擎模板
    */
    public function Plates()
    {
        require $this->_dir.'Autoloader.php';
        \Paltes_AutoLoader::register();
        // 生成option
        if(is_array($this->_option)) $opt = $this->_option;
        else $opt = config('Plates_opt');
        $suffix = isset($opt['suffix']) && !empty($opt['suffix'])? $opt['suffix']:'html';
        $m = new \League\Plates\Engine($opt['view_dir'],$suffix);
        return $m;
    }
}